package com.dk.controller;

import com.dk.api.ItemServiceAPI;
import com.dk.api.UserServiceAPI;
import com.dk.pojo.MdItem;
import com.dk.pojo.MdVendor;
import com.dk.pojo.QcIqc;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.IqcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@RestController
@RequestMapping("iqc")
@Api(tags = "来料检验接口文档")
@RefreshScope
public class IqcController {

    @Autowired
    private IqcService iqcService;
    @Autowired
    private UserServiceAPI userServiceAPI;
    @Autowired
    private ItemServiceAPI itemServiceAPI;

    @GetMapping("getIqcInfo")
    @ApiOperation("来料检验模糊查询和分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数"),
            @ApiImplicitParam(name = "iqcCode", value = "检验单编号"),
            @ApiImplicitParam(name = "vendorCode", value = "供应商编码"),
            @ApiImplicitParam(name = "vendorBatch", value = "供应商批次号"),
            @ApiImplicitParam(name = "itemCode", value = "产品物料编码"),
            @ApiImplicitParam(name = "checkResult", value = "检测结果"),
    })
    public Result getIqcInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             String iqcCode, String vendorCode, String vendorBatch,
                             String itemCode,String checkResult){
        PageResult<QcIqc> result = iqcService.getIqcInfo(pageNum,pageSize,iqcCode,vendorCode,vendorBatch,itemCode,checkResult);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addIqc")
    @ApiOperation("来料检验增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iqcCode",value = "检验单编号",required = true),
            @ApiImplicitParam(name = "iqcName",value = "检验单名称",required = true),
            @ApiImplicitParam(name = "itemCode",value = "产品物料编码",required = true),
            @ApiImplicitParam(name = "vendorCode",value = "供应商编码",required = true),
            @ApiImplicitParam(name = "quantityRecived",value = "本次接收数量",required = true),
            @ApiImplicitParam(name = "quantityCheck",value = "本次检测数量",required = true),
            @ApiImplicitParam(name = "reciveDate",value = "来料日期",required = true),
            @ApiImplicitParam(name = "inspectDate",value = "检测日期",required = true),
            @ApiImplicitParam(name = "vendorBatch",value = "供应商批次号",required = true),
            @ApiImplicitParam(name = "itemId",value = "产品物料ID",required = false),
            @ApiImplicitParam(name = "vendorId",value = "供应商ID",required = false),
            @ApiImplicitParam(name = "checkResult", value = "检测结果",required = true),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result addIqc(QcIqc iqc, @RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        //根据供应商编码查询出供应商信息
        MdVendor vendor = userServiceAPI.selectByVendorCode(iqc.getVendorCode());
        if(vendor == null){
            return new Result(ResultConstant.IQC_ADD_NULL, ResultConstant.IQC_ADD_MESSAGE);
        }
        //根据产品物料编码查询出产品物料信息
        MdItem item = itemServiceAPI.selectByItemCode(iqc.getItemCode());
        if(item == null){
            return new Result(ResultConstant.IQC_ADD_NULL, ResultConstant.IQC_ADD_MESSAGE);
        }
        return iqcService.addIqc(iqc,vendor,item,token);
    }


    @PutMapping("updateIqc")
    @ApiOperation("来料检验修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iqcId",value = "检验单id",required = true),
            @ApiImplicitParam(name = "iqcCode",value = "检验单编号"),
            @ApiImplicitParam(name = "iqcName",value = "检验单名称"),
            @ApiImplicitParam(name = "itemCode",value = "产品物料编码"),
            @ApiImplicitParam(name = "vendorCode",value = "供应商编码"),
            @ApiImplicitParam(name = "quantityRecived",value = "本次接收数量"),
            @ApiImplicitParam(name = "quantityCheck",value = "本次检测数量"),
            @ApiImplicitParam(name = "reciveDate",value = "来料日期"),
            @ApiImplicitParam(name = "inspectDate",value = "检测日期"),
            @ApiImplicitParam(name = "vendorBatch",value = "供应商批次号"),
            @ApiImplicitParam(name = "itemId",value = "产品物料ID"),
            @ApiImplicitParam(name = "vendorId",value = "供应商ID"),
            @ApiImplicitParam(name = "checkResult", value = "检测结果"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result updateIqc(QcIqc iqc, @RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        if(iqc.getItemCode() == null &&  iqc.getVendorCode() == null){
            return iqcService.updateIqc(iqc,token);
        }else if(iqc.getItemCode() != null &&  iqc.getVendorCode() == null){
            MdItem item = itemServiceAPI.selectByItemCode(iqc.getItemCode());
            return iqcService.updateIqc(iqc,item,token);
        }else if(iqc.getItemCode() == null &&  iqc.getVendorCode() != null){
            MdVendor vendor = userServiceAPI.selectByVendorCode(iqc.getVendorCode());
            return iqcService.updateIqc(iqc,vendor,token);
        }else {
            MdVendor vendor = userServiceAPI.selectByVendorCode(iqc.getVendorCode());
            MdItem item = itemServiceAPI.selectByItemCode(iqc.getItemCode());
            return iqcService.updateIqc(iqc,vendor,item,token);
        }
    }

    @DeleteMapping("deleteIqc")
    @ApiOperation("来料检验删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iqcId", value = "来料检验id",required = true)
    })
    public Result deleteIqc(Long iqcId){
        iqcService.deleteIqc(iqcId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

    @GetMapping("selectByIqcCode")
    public QcIqc selectByIqcCode(@RequestParam String iqcCode){
        return iqcService.selectByIqcCode(iqcCode);
    }
}